package com.xuanmiao.smsback.service.scheduled;

import com.xuanmiao.smsback.dao.mapper.MessagePushHistoryMapper;
import com.xuanmiao.smsback.dao.mapper.UserAccountInfoMapper;
import com.xuanmiao.smsback.dao.mapper.UserInfoMapper;
import com.xuanmiao.smsback.dao.model.MessagePushHistoryDO;
import com.xuanmiao.smsback.dao.model.UserAccountInfoDO;
import com.xuanmiao.smsback.dao.model.UserInfoDO;
import com.xuanmiao.smsback.domain.qo.UserInfoQO;
import com.xuanmiao.smsback.service.email.MailService;
import com.xuanmiao.smsback.service.email.event.SendEmailEvent;
import com.xuanmiao.smsback.service.email.event.SendEmailObject;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 充值提醒
 */
@Component
@JobHandler(value = "rechargeNotifyService")
@Slf4j
public class RechargeNotifyService extends IJobHandler {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    private UserAccountInfoMapper userAccountInfoMapper;

    @Autowired
    MessagePushHistoryMapper messagePushHistoryMapper;

    @Autowired
    private ApplicationContext applicationContext;


    @Autowired
    @Qualifier("aliyunEmailServiceImpl")
    private MailService mailService;

    @Override
    public ReturnT<String> execute(String param) {
        // 查询所有的注册用户；
        List<UserInfoDO> userList = userInfoMapper.selectAll();
        Map<Integer,UserInfoDO> mainUserMap = new HashMap<>();
        userList.stream().forEach(user->{
            if(user.getIsMain() && user.getStatus() == 1){
                mainUserMap.put(user.getId(),user);
            }
        });
        if (CollectionUtils.isNotEmpty(userList)) {
            // 充值提醒；
            userList.stream().forEach(user -> {
                if(!(user.getStatus() == 1)){
                    return ;
                }
                Integer mainUserId = null;
                if(user.getIsMain()){
                    mainUserId = user.getId();
                }else{
                    mainUserId = user.getMainUserId();
                }
                UserAccountInfoDO userAccountInfo = userAccountInfoMapper.selectByPrimaryKey(mainUserId);
                if (userAccountInfo.getBalance() < 500) {
                    // 消费通知入库
                    MessagePushHistoryDO messagePushHistoryDO = new MessagePushHistoryDO();
                    messagePushHistoryDO.setStatus(0);
                    messagePushHistoryDO.setTitle("充值提醒");
                    messagePushHistoryDO.setText("您的账户余额低于500RMB，请尽快充值！");
                    messagePushHistoryDO.setUserId(user.getId());
                    messagePushHistoryMapper.insertSelective(messagePushHistoryDO);

                    if(StringUtils.isNotBlank(user.getEmail()) && user.getIsMain()){
//                        SendEmailObject object = new SendEmailObject();
//                        object.setTo(Stream.of(user.getEmail()).collect(Collectors.toList()));
//                        object.setSubject("SMS账户余额过低提醒");
//                        StringBuilder sb = new StringBuilder("");
//                        sb.append("尊敬的商户")
//                                .append(user.getBusinessNo())
//                                .append("\r\n")
//                                .append("您好")
//                                .append("\r\n")
//                                .append("您的账户")
//                                .append("【")
//                                .append(user.getAccountNo())
//                                .append("】")
//                                .append("余额低于500RMB，请尽快充值！以免影响您的正常使用！");
//                        object.setContent(sb.toString());
//                        SendEmailEvent event = new SendEmailEvent(applicationContext,object);
//                        applicationContext.publishEvent(event);

                        StringBuilder sb = new StringBuilder("<html><body>");
                        String subject = "SMS账户余额过低提醒";
                        sb.append("尊敬的商户")
                                .append(user.getBusinessNo())
                                .append("<br/>")
                                .append("您好")
                                .append("<br/>")
                                .append("您的账户")
                                .append("【")
                                .append(user.getAccountNo())
                                .append("】")
                                .append("余额低于500RMB，请尽快充值！以免影响您的正常使用！")
                                .append("</body></html>");

                        mailService.sendHtmlMail(user.getEmail(),subject,sb.toString());
                    }
                }

            });
        }
        return ReturnT.SUCCESS;
    }
}
